//
//  CommentTableViewCell.m
//  Molian
//
//  Created by 胡津 on 15/3/27.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "CommentTableViewCell.h"
#import "WEBViewController.h"
@implementation CommentTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setNeedsLayout];
        [self setNeedsDisplay];
        
        //用户头像
        self.userAvatarImg = [[UIImageView alloc]init];
        self.userAvatarImg.frame = CGRectMake(10, 10, 40, 40);
        self.userAvatarImg.layer.masksToBounds = YES;//开启圆角
        [self.userAvatarImg.layer setCornerRadius:7];
        [self.userAvatarImg setContentMode:UIViewContentModeScaleAspectFill];
        [self.contentView addSubview:self.userAvatarImg];
        
        
        //用户名字
        self.userNameLabel = [[UILabel alloc]init];
        self.userNameLabel.font = [UIFont fontWithName:FontName size:14];
//        userNameLabel.tag = indexPath.row+100;
        self.userNameLabel.frame = CGRectMake(55, 6, DeviceWidth-60, 30);
        if (self.userNameLabel.bounds.size.width >= DeviceWidth-200) {
            self.userNameLabel.frame = CGRectMake(55, 8, DeviceWidth-205, 30);
        }
        [self.contentView addSubview:self.userNameLabel];

        
        //用户地址+星座
        self.userCityLabel = [[UILabel alloc]init];
        self.userCityLabel.frame = CGRectMake(55, 35, 35, 12);
        self.userCityLabel.textColor = [UIColor grayColor];
        self.userCityLabel.font = [UIFont fontWithName:FontName size:11];
        [self.contentView addSubview:self.userCityLabel];
        
        //时间
        self.userTimeLabel = [[UILabel alloc]init];
        self.userTimeLabel.frame = CGRectMake(90, 35, 150, 12);
        self.userTimeLabel.textColor = [UIColor grayColor];
        self.userTimeLabel.textAlignment = NSTextAlignmentLeft;
        self.userTimeLabel.font = [UIFont fontWithName:FontName size:11];
        [self.contentView addSubview:self.userTimeLabel];
        
        
        self.CommentCLabel = [[KZLinkLabel alloc] init];
        self.CommentCLabel.automaticLinkDetectionEnabled = YES;
        self.CommentCLabel.font = [UIFont fontWithName:FontName size:13];
        self.CommentCLabel.textColor = RGBA(51, 51, 51, 1);
        self.CommentCLabel.numberOfLines = 0;
        self.CommentCLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.CommentCLabel sizeToFit];
        self.CommentCLabel.linkColor = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
        self.CommentCLabel.linkHighlightColor = [UIColor orangeColor];
        [self.contentView addSubview:self.CommentCLabel];
        
        __weak __typeof(self) weakSelf = self;

        self.CommentCLabel.linkTapHandler = ^(KZLinkType linkType, NSString *string, NSRange range){
            if (linkType == KZLinkTypeURL) {
                [weakSelf openURL:[NSURL URLWithString:string]];
            } else if (linkType == KZLinkTypePhoneNumber) {
                [weakSelf openTel:string];
            } else if (linkType == KZLinkTypeHashTag) {
                [HUD Prompt:string];
            } else {
                NSLog(@"Other Link");
            }
        };
        
        self.CommentCLabel.linkLongPressHandler = ^(KZLinkType linkType, NSString *string, NSRange range){
            NSMutableDictionary *linkDictionary = [NSMutableDictionary dictionaryWithCapacity:3];
            [linkDictionary setObject:@(linkType) forKey:@"linkType"];
            [linkDictionary setObject:string forKey:@"link"];
            [linkDictionary setObject:[NSValue valueWithRange:range] forKey:@"range"];
            weakSelf.selectedLinkDic = linkDictionary;
            
            NSString *openTypeString;
            if (linkType == KZLinkTypeURL) {
                openTypeString = @"在Safari中打开";
            } else if (linkType == KZLinkTypePhoneNumber) {
                openTypeString = @"直接拨打";
            }
            
            HJActionSheet *sheet = [[HJActionSheet alloc]initWithTitle:@"这是一个网址" Delegate:weakSelf destructiveButtonTitle:@"取消" OtherButtonTitles:@[@"拷贝",openTypeString]];
            [sheet show];

        };

        
    }
    
    return self;
}

#pragma mark - HJActionSheet Sheet Delegate
-(void)hjActionSheet:(UIButton *)sender clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (!self.selectedLinkDic) {
        return;
    }
    switch (buttonIndex)
    {
        case 0:
        {
            [UIPasteboard generalPasteboard].string = self.selectedLinkDic[@"link"];
            break;
        }
        case 1:
        {
            KZLinkType linkType = [self.selectedLinkDic[@"linkType"] integerValue];
            if (linkType == KZLinkTypeURL) {
                NSURL *url = [NSURL URLWithString:self.selectedLinkDic[@"link"]];
                [self openURL:url];
            } else if (linkType == KZLinkTypePhoneNumber) {
                [self openTel:self.selectedLinkDic[@"link"]];
            }
            break;
        }
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    if (_gender ==1 ) { //男
        self.userNameLabel.textColor = RGBA(36, 158, 228, 1);
    }else if (_gender == 2){//女
        self.userNameLabel.textColor = RGBA(241, 114, 156, 1);
    }
    
    self.userCityLabel.frame = CGRectMake(55, 35, [Utils LabeSizeText:self.userCityLabel.text Font:[UIFont fontWithName:FontName size:11]].width, 12);
    self.userTimeLabel.frame = CGRectMake([Utils LabeSizeText:self.userCityLabel.text Font:[UIFont fontWithName:FontName size:11]].width+60 , 35, 150, 12);

}


- (BOOL)openURL:(NSURL *)url
{
    BOOL safariCompatible = [url.scheme isEqualToString:@"http"] || [url.scheme isEqualToString:@"https"];
    if (safariCompatible){
//        [[UIApplication sharedApplication] openURL:url];
        WEBViewController *webvc = [[WEBViewController alloc]init];
        webvc.WEBURL = [NSString stringWithFormat:@"%@",url];
        UINavigationController *webvcNav = [[UINavigationController alloc]initWithRootViewController:webvc];
        webvcNav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self.window.rootViewController presentViewController:webvcNav animated:YES completion:nil];
        return YES;
    } else {
        return NO;
    }
}
- (BOOL)openTel:(NSString *)tel
{
    NSString *telString = [NSString stringWithFormat:@"tel://%@",tel];
    return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
